package com.obtk.shilipai.controller;

import com.obtk.shilipai.entity.Dishe;
import com.obtk.shilipai.entity.DisheType;
import com.obtk.shilipai.entity.Shop;
import com.obtk.shilipai.service.DisheService;
import com.obtk.shilipai.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpContext;
import java.util.List;

@RestController
@RequestMapping("api")
public class ShopApi {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ShopService shopService;
    @Autowired
    private DisheService disheService;

    @RequestMapping("/getShopByAll")
    @ResponseBody
    public List<Shop> getShopByAll(String shopName){
        List<Shop> shop = shopService.selectShopByAll(shopName);
        String scheme = request.getScheme(); // 获取协议名，如 http、https
        String serverName = request.getServerName(); // 获取主机名，如 localhost
        int serverPort = request.getServerPort(); //
        String dizhi=scheme+"://" + serverName + ":" + serverPort+"/";
        for (int i=0;i<shop.size();i++){
            shop.get(i).setShopImage(dizhi+shop.get(i).getShopImage());
        }
        return shop;
    }

    @RequestMapping("/getDisheType")
    @ResponseBody
    public List<DisheType> getDisheType(Integer shopId){
        List<DisheType> disheType = disheService.getDisheType(shopId);
        return disheType;
    }

    @RequestMapping("/getShop")
    @ResponseBody
    public Shop getShopByAll(Integer shopId){
        System.out.println(shopId);
        Shop shop = shopService.getShopByAll(shopId);

        String scheme = request.getScheme(); // 获取协议名，如 http、https
        String serverName = request.getServerName(); // 获取主机名，如 localhost
        int serverPort = request.getServerPort(); //
        String dizhi=scheme+"://" + serverName + ":" + serverPort+"/";
        shop.setShopImage(dizhi+shop.getShopImage());

        return shop;
    }
    @RequestMapping("/getDishe")
    @ResponseBody
    public List<Dishe> getDishe(Integer shopId){
        System.out.println(shopId);
        List<Dishe> dishe = disheService.getDisheByShopId(shopId);

        String scheme = request.getScheme(); // 获取协议名，如 http、https
        String serverName = request.getServerName(); // 获取主机名，如 localhost
        int serverPort = request.getServerPort(); //
        String dizhi=scheme+"://" + serverName + ":" + serverPort+"/"+"dishe/";

        for (int i=0;i<dishe.size();i++){
            dishe.get(i).setDisheImage(dizhi+dishe.get(i).getDisheImage());
        }
        System.out.println(dishe);


        return dishe;
    }
}
